//
//  NewsListViewController.m
//  网易新闻
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 马文强. All rights reserved.
//

#import "NewsListViewController.h"
#import "NewsModel.h"
#import "FDSlideBar.h"
#import "netWorkHandle.h"
#import "UIImageView+WebCache.h"
#import "TwoNewsListTableViewCell.h"
#import "NewsListTableViewCell.h"
#import "NewsDetailViewController.h"
#import "NewsDetailTwoViewController.h"
#import "MJRefresh.h"

@interface NewsListViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *NewsTitleArray;
@property (strong, nonatomic) UIScrollView *scrollVeiw;
@property (strong, nonatomic) NSMutableArray *NewsTnameArray;
@property (assign, nonatomic) int PagNum;




@end

@implementation NewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    [self loadtableView];
    [self loadscrollVeiw];
    [self loadNavigation];
    [self loadData];
    [self loadMoreData];
    

}

#pragma mark -----

- (void)loadData
{
    __weak __typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _PagNum = 20;
        [weakSelf loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
    }];
    [self.tableView.header beginRefreshing];
}


- (void)loadMoreData
{
    
    __weak __typeof(self) weakSelf = self;
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _PagNum += 10;
        [weakSelf loadNetWorkData:[NSString stringWithFormat:@"%d", _PagNum]];
        
    }];
}

- (void)loadNavigation
{
    self.navigationItem.title = @"新闻";
}

- (void)loadNetWorkData:(NSString *)string{
    

    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-%@.html",string];

    [NetWorkHandle getDataWithURLString:str compare:^(id object) {
        NSDictionary *dic = object;
        NSArray *arr = [dic objectForKey:@"T1348647853363"];
        _NewsTnameArray = [NSMutableArray array];
         for (NSDictionary *dictionary in arr) {
             NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.NewsTnameArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

// 清除缓存
- (void)cleanNewsTnameArray
{
    _NewsTnameArray = nil;
}


#pragma mark --- 初始化scrollView
- (void)loadscrollVeiw{
    //初始化UIScrollView alloc
    self.scrollVeiw = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    //是否整平翻动
    self.scrollVeiw.pagingEnabled = YES;
    //边缘弹动效果
    self.scrollVeiw.bounces = NO;
    //指定代理人
    self.scrollVeiw.delegate = self;
    self.scrollVeiw.backgroundColor = [UIColor cyanColor];
    self.scrollVeiw.showsHorizontalScrollIndicator = NO;
    self.tableView.tableHeaderView = _scrollVeiw;
    [self.view addSubview:self.tableView];
     _NewsTitleArray = [NSMutableArray array];
    [NetWorkHandle getDataWithURLString:@"http://c.m.163.com/nc/ad/headline/0-4.html" compare:^(id object) {
        NSDictionary *dic = object;
        NSArray *arr = [dic objectForKey:@"headline_ad"];
        for (NSDictionary *dictionary in arr) {
            
            NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [_NewsTitleArray addObject:model];
        }
        
        for (int i = 0; i < _NewsTitleArray.count; i ++) {
            NewsModel *model = _NewsTitleArray[i];
            UIImageView* HeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
             [HeaderImageView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
            [_scrollVeiw addSubview:HeaderImageView];
        }
        //确保数据拿到之后 才去设置UIScrollveiw的contentsize
        self.scrollVeiw.contentSize = CGSizeMake(self.view.frame.size.width * _NewsTitleArray.count, 0);
    }];
}

#pragma mark --- 初始化tableView
- (void)loadtableView
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30) style:UITableViewStylePlain];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TwoNewsListTableViewCell class] forCellReuseIdentifier:@"cell"];
    UINib *nib = [UINib nibWithNibName:@"NewsListTableViewCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"CELL"];
    
    
}

#pragma mark - TableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _NewsTnameArray.count;
}

#pragma mark ---- 在这里判断cell的样子
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     NewsModel *model = self.NewsTnameArray[indexPath.row];
    if (model.imgextra != nil) {
        TwoNewsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    else{
        
        NewsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return _scrollVeiw;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 140;

}

#pragma mark - cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *model = _NewsTnameArray[indexPath.row];
    if (model.imgextra == nil) {
        NewsDetailViewController *detailVC = [[NewsDetailViewController alloc] init];
        detailVC.webStr = model.url_3w;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else
    {
        NewsDetailTwoViewController *twoDetailVC = [[NewsDetailTwoViewController alloc] init];
        
        // 三张图片的显示
        NSString *flag = [model.photosetID substringFromIndex:9];
        NSRange range = {4, 4};
        NSString *flag1 = [model.photosetID substringWithRange:range];
        NSString *urlStr = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@/%@.json",flag1 , flag];
        twoDetailVC.threeImage = urlStr;
        [self.navigationController showDetailViewController:twoDetailVC sender:self];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
